#pragma once

/**
*	Class Bit for representing a sinlge Bit as a binary number, 0 or 1.
*	The class contains methods for several logic gates (AND,OR etc.) and a method for adding one bit to another with carry in and carry out.
*/

class Bit
{
private:
	char value;
public:

	Bit(void)
	{
		this->value = '0';
	}

	Bit( char value )
	{
		this->value = value;
	}

	~Bit(void)
	{
	}

	Bit and( Bit b )
	{
		if( this->GetValue() == '1' && b.GetValue() == '1' )
			return Bit('1');
		else
			return Bit('0');
	}

	Bit or( Bit b )
	{
		if( this->GetValue() == '1' || b.GetValue() == '1' )
			return Bit('1');
		else
			return Bit('0');
	}

	Bit xor( Bit b )
	{
		if( this->GetValue() == '1' && b.GetValue() == '0' )
			return Bit('1');
		else if( this->GetValue() == '0' && b.GetValue() == '1' )
			return Bit('1');
		else
			return Bit('0');
	}

	Bit not( )
	{
		if( this->GetValue() == '1' )
			return Bit('0');
		else
			return Bit('1');
	}

	Bit nand( Bit b )
	{
		return this->and( b ).not();
	}

	Bit nor( Bit b)
	{
		return this->or( b ).not();
	}


	void add( Bit b, Bit ci, Bit *sum, Bit *cu)
	{
		Bit n1,n2,n3,n4;

		n4 = this->and(b);
		n2 = this->or(b);
		n3 = n2.and(ci);
		n1 = this->xor(b);
		*cu = n4.or(n3);
		*sum = n1.xor(ci);

	}

	char GetValue() { return this->value; }
	void SetValue( char value ) { this->value = value; }
};